#include <QFile>
#include <QTextCodec>
#include <QStringList>
#include <openssl/aes.h>
#include "settings.h"
#include "profile.h"

Profile::Profile(QObject *parent) : QObject(parent)
{

}

Profile::~Profile()
{

}

//void Profile::SetSettingsManager(QSettings *set)
//{
//    settings=set;
//}

void   Profile::SaveToFile(QString file_name, unsigned char *key)
{
    QString defaultname=file_name;
    if(defaultname=="") defaultname=settings->value(settings_key).toString();

    QFile file(defaultname);
    //if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    if (!file.open(QIODevice::WriteOnly))
        return ;

    m_file_name=defaultname;
    QString *write_str=StringFromParameters();
//    QString write_str=m_user_name+"||"
//                     +m_user_login+"||"
//                     +m_password+"||"
//                     +m_provider_name+"||"
//                     +m_balance+"||"
//                     +QString::number(m_login_type);

    unsigned char enc_str[256]={0};
    unsigned char raw_str[256]={0};

    QTextCodec *codec51 = QTextCodec::codecForName("CP-1251");
    QTextCodec::setCodecForLocale(codec51);

    strcpy((char *)raw_str, write_str->toLocal8Bit().data());
    SetStr(key, raw_str, enc_str, write_str->size());
    file.write((const char*)enc_str,write_str->size());
    file.close();
    settings->setValue(settings_key, defaultname);
    m_file_name=file_name;
}

void  Profile::LoadFromFile(QString file_name, unsigned char *key)
{
    QFile file(file_name);
    //if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    if (!file.open(QIODevice::ReadOnly))
        return ;

    QByteArray arr=file.readAll();
    file.close();

    QStringList *profileInfo=new QStringList(*GetInfoDecrypted(key, arr));
    if(profileInfo)
    {
        SaveParamsFromList(*profileInfo);
        delete profileInfo;
    }
    settings->setValue(settings_key, file_name);
    m_file_name=file_name;
}

bool    Profile::SetParameters(QStringList &params, unsigned char key[16])
{
    if(params.size()!=parameters_size)
        return false;
    QFile file(m_file_name);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return false;

//    m_user_name=params[0];
//    m_user_login=params[1];
//    m_password=params[2];
//    m_provider_name=params[3];
    SaveParamsFromList(params);

    //QTextStream out(&file);
    unsigned char enc_str[256]={0};
    unsigned char raw_str[256]={0};
    QString write_str(params.join("||"));

    QTextCodec *codec51 = QTextCodec::codecForName("CP-1251");
    QTextCodec::setCodecForLocale(codec51);

    strcpy((char *)raw_str, write_str.toLocal8Bit().data());
    SetStr(key, raw_str, enc_str, write_str.size());
    file.write((const char*)enc_str,write_str.size());
    file.close();
    return true;
}

QStringList *Profile::GetInfoFromFile(unsigned char *key)
{
    unsigned char decrypted_str[512]={0};
    unsigned char encrypted_str[512]={0};

//in file there is a string like: "user||passwod||provider"
    QFile file(m_file_name);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return NULL;

    int str_len=file.readLine((char *)encrypted_str, sizeof(encrypted_str));
    if(str_len==-1)
    {
        file.close();
        return NULL;
    }
    else
    {
        GetStr(key, encrypted_str, decrypted_str, str_len);
        QString str((const char*)decrypted_str);
        QStringList *list = new QStringList (str.split("||"));
        return list;
    }
    return NULL;
}

QStringList *Profile::GetInfoDecrypted(unsigned char key[16], QByteArray &encrypted_arr)
{
    unsigned char decrypted_str[512]={0};
    //unsigned char enc_str[512]={0};
    GetStr(key, (unsigned char *)encrypted_arr.data(), decrypted_str, encrypted_arr.length());
    QByteArray arr=QByteArray::fromRawData((char *)decrypted_str,encrypted_arr.length());
    QTextCodec *codec = QTextCodec::codecForName("CP-1251");
    QTextCodec::setCodecForLocale(codec);
    QString str(codec->toUnicode(arr));
    QStringList *list = new QStringList (str.split("||"));
    return list;
}


void    Profile::GetStr(unsigned char *key,
                                       unsigned char *in_str,
                                       unsigned char *out_str, unsigned int lenght)
{
    unsigned char ivecstr[AES_BLOCK_SIZE] = "goodbyworldkey\0";
    //unsigned char ivec[AES_BLOCK_SIZE];
    //memcpy( ivec , ivecstr, AES_BLOCK_SIZE);

    AES_KEY enc_key;
    AES_set_encrypt_key(key,128,&enc_key);


    int num=0;

    AES_cfb128_encrypt(in_str, out_str, lenght, &enc_key, ivecstr, &num,
               AES_DECRYPT);
}

void Profile::SetStr(unsigned char *key,
                            unsigned char *in_str,
                            unsigned char *out_str, unsigned int lenght)
{
    unsigned char ivecstr[AES_BLOCK_SIZE] = "goodbyworldkey\0";
    //unsigned char ivec[AES_BLOCK_SIZE];
    //memcpy( ivec , ivecstr, AES_BLOCK_SIZE);

    AES_KEY enc_key;
    AES_set_encrypt_key(key,128,&enc_key);


    int num=0;

    AES_cfb128_encrypt(in_str, out_str, lenght, &enc_key, ivecstr, &num,
               AES_ENCRYPT);

}
